interface ShouleBeObjectResult {
    res: boolean;
    msg?: string;
  }
  interface GetParameterErrorOption {
    correct: string;
    wrong: any;
  }
  export function successHandler(
    success?: (res: CallbackResult) => void,
    complete?: (res: CallbackResult) => void,
  ) {
    return function (res: CallbackResult): Promise<any> {
      success && success(res);
      complete && complete(res);
      return Promise.resolve(res);
    };
  }
  
  export function errorHandler(
    fail?: (res: CallbackResult) => void,
    complete?: (res: CallbackResult) => void,
  ) {
    return function (res: CallbackResult): Promise<any> {
      fail && fail(res);
      complete && complete(res);
      return Promise.reject(res);
    };
  }
  function upperCaseFirstLetter(string: string): string {
    return string.replace(/^./, (match) => match.toUpperCase());
  }
  
  export function getParameterError({
    correct,
    wrong,
  }: GetParameterErrorOption): string {
    const parameter = 'parameter';
    const errorType = upperCaseFirstLetter(
      wrong === null ? 'Null' : typeof wrong,
    );
    return `fail parameter error: ${parameter} should be ${correct} instead of ${errorType}`;
  }
  
  export function shouleBeObject(target: unknown): ShouleBeObjectResult {
    if (target && typeof target === 'object') return {res: true};
    return {
      res: false,
      msg: getParameterError({
        correct: 'Object',
        wrong: target,
      }),
    };
  }
  